package com.cnfangmao.masterdata.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
	private MD5Utils() {
	}

	/**
	 * 获取input对应的MD5代码
	 * @param input
	 * @return
	 */
	public static final String getMD5(String input) {
		try {
			byte[] inputByte = input.getBytes();
			MessageDigest md;

			md = MessageDigest.getInstance("md5");

			md.update(inputByte);
			byte[] digest = md.digest();
			StringBuffer buf = new StringBuffer();
			for (int i = 0; i < digest.length; i++) {
				int val = (digest[i]) & 0xff;
				if (val < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(val));
			}
			return buf.toString();
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	public static final String getMD5(String input,String charset) {
		try {
			byte[] inputByte;
			try {
				inputByte = input.getBytes(charset);
			} catch (UnsupportedEncodingException e) {
				System.err.println(e.getMessage());
				return "";
			}
			MessageDigest md;

			md = MessageDigest.getInstance("md5");

			md.update(inputByte);
			byte[] digest = md.digest();
			StringBuffer buf = new StringBuffer();
			for (int i = 0; i < digest.length; i++) {
				int val = (digest[i]) & 0xff;
				if (val < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(val));
			}
			return buf.toString();
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	
	public static final String getMD516(String input){
		String md5 = getMD5(input);
		return md5.substring(8,24);
	}
	public static final String getMD532(String input){
		return getMD5(input);
	}

}
